console.log($event, '$event');
const helper = new Helper(this)
const prefix = helper.getVariable('environmentType');
const stream = StreamServices.getInstance(prefix)

const { dataTransfer } = $event;

async function handleAreaData(projectAreaCode, projectCode) {
    const { areaRows, equipmentRows, fusesRows } = await _videoAccess.queryQuotaByArea(projectAreaCode, projectCode);
    _videoAccess.renderTree([...areaRows, ...equipmentRows, ...fusesRows], projectAreaCode);
}

const handleFuse = async (data, cb = () => {}) => {
    const res = await _videoAccess.getFuseByEquipmentInfo(data);
    if (!res) return;
    cb();
    _videoAccess.addPlayer({
        ...res,
        playerType: 'webrtc',
        equipmentName: '融合预览',
        url: `/${prefix}streamUrl/api/v1/stream/rtc/play`
    });
}


if (!dataTransfer?.getData('data:source')) {
    layer.msg('未找到摄像头配置!');
} else {
    const index = $($event.target).closest('.sullivan-grid-item').data('index')
    const widgets = helper.get('三维组件');
    const sullivan = widgets.widgetObj.sullivan;
    const source = JSON.parse(dataTransfer?.getData('data:source'));

    // console.log('monitorSelect: => ', source);
    if (source.children) {
        layer.msg('未找到摄像头配置!');
    } else {
        const oldVideo = sullivan.cameraMap.get(index)
        if (oldVideo) {
            const { projectAreaCode, projectCode } = oldVideo;
            layer.confirm('<dl><dt><h3>是否需要视频拼接？</h3></dt><!--<dd>拼接成功将融合两个视频。</dd>--></dl>', { icon: 3, title: '视频拼接' }, () => {
                handleFuse([oldVideo.data, source], () => {
                    // 关闭旧的摄像头
                    sullivan.close(index);
                    // 保存拼接
                    _videoAccess.insertVideoFuse([oldVideo.data, source]).then(res => {
                        handleAreaData(projectAreaCode, projectCode)
                        layer.msg('视频融合成功!');
                    });
                })
            }, function(){
                layer?.msg('取消');
            });
        } else {
            _videoAccess.addPlayer(source);
        }
    }
    // console.log(source, 'source')
}